package com.sdry.web.controller.gemini;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.PartStore;
import com.sdry.entity.PartStoreExample;
import com.sdry.entity.PartStoreExample.Criteria;
import com.sdry.entity.PartStoreWithBLOBs;
import com.sdry.entity.permission.UpmsStaffRole;
import com.sdry.service.gemini.PartStoreService;
import com.sdry.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Gemini on 2018/7/31
 *
 * 仓库信息控制类
 */

@Controller
@RequestMapping("/base/warehouse")
public class BaseWarehouseInfoController extends BaseController {

    @Resource
    private PartStoreService storeService;

    /**
     * 去仓库信息页面
     * @return
     */
    @RequestMapping(value = "/baseInfoWarehouse", method = RequestMethod.GET)
    public String baseInfoWarehouse() {

        return "/gemini/info_warehouse";
    }

    /**
     * 添加仓库信息
     * @param response 响应
     * @param partStore 仓库实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertBaseWarehouseInfo")
    public void insertWarehouse(HttpServletRequest request,HttpServletResponse response, PartStoreWithBLOBs partStore) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        partStore.setLocationptime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        partStore.setLocationpeople(name);
        //添加
        int affact = storeService.insert(partStore);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询仓库信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param partStore 仓库实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseWarehouseInfo")
    public void selectWarehouseList(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "limit",required = false)int limit,
                                     @RequestParam(value = "offset",required = false)int offset,
                                     PartStore partStore) throws Exception {

        PageHelper.startPage(offset, limit);

        PartStoreExample example = new PartStoreExample();
        Criteria criteria = example.createCriteria();
        if (partStore.getLocationname() != null && !("").equals(partStore.getLocationname())){
            criteria.andLocationnameLike("%"+partStore.getLocationname()+"%");
        }

        if (partStore.getLocationnumbering() != null && !("").equals(partStore.getLocationnumbering())){
            criteria.andLocationnumberingLike("%"+partStore.getLocationnumbering()+"%");
        }


        List<PartStore> partStores = storeService.selectByExample(example);

        PageInfo pageInfo = new PageInfo(partStores);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", pageInfo.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 查询所有仓库信息
     * @param request 请求
     * @param response 响应
     * @param partStore 仓库实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectAllWarehouse")
    public void selectAllWarehouse(HttpServletRequest request, HttpServletResponse response,
                                    PartStore partStore) throws Exception {

        PartStoreExample example = new PartStoreExample();
        List<PartStore> partStores = storeService.selectByExample(example);
        JSONArray array=JSONArray.fromObject(partStores);
        ResponseUtil.write(response, array);
    }


    /**
     * 根据id查询仓库信息
     * @param response 响应
     * @param id 仓库id
     * @throws Exception 异常
     */
    @RequestMapping("/selectBaseWarehouseInfoById")
    public void selectWarehouseById(HttpServletResponse response, Long id) throws Exception {

        PartStore partStore = storeService.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(partStore);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改仓库信息
     * @param response 响应
     * @param partStore 仓库信息
     * @throws Exception 异常
     */
    @RequestMapping("/editBaseWarehouseInfo")
    public void editWarehouse( HttpServletRequest request,HttpServletResponse response,
                               PartStore partStore) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        partStore.setLocationptime(df.format(new Date()));//给录入时间赋值
        //判断session
        HttpSession session  = request.getSession();
        //从session中取出用户身份信息
        UpmsStaffRole staffSession = (UpmsStaffRole) session.getAttribute("user");

        String name = null;
        if(staffSession != null){
            name = staffSession.getUpmsStaff().getSname();
        }
        partStore.setLocationpeople(name);
        //修改
        int affct = storeService.updateByPrimaryKey(partStore);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除仓库信息
     * @param response 响应
     * @param ids 仓库id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteBaseWarehouseInfo")
    public void deleteWarehouse( HttpServletResponse response, String ids) throws Exception {
        int affact = storeService.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
}
